package com.woniuxy.rateassembly.service.impl;

import com.woniuxy.form.CommentForm;
import com.woniuxy.rateassembly.model.Comment;
import com.woniuxy.rateassembly.dao.CommentMapper;
import com.woniuxy.rateassembly.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2021-05-27
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {


    @Autowired
    private CommentMapper commentMapper;


    @Override
    public void addComment(CommentForm commentForm) {

        Comment comment = new Comment();
        //按照同名字段拷贝数据
        BeanUtils.copyProperties(commentForm,comment);
        commentMapper.insert(comment);
    }


    @Override
    @Cacheable(value="user_cache",key = "'user_'+#UserId")
    public List<Comment> findCommentByUserId(Integer UserId) {
        log.info("根据用户id为{}从数据库查询",UserId);
        List<Comment> comments = commentMapper.findCommentByUserId(UserId);
        return comments;
    }

    @Override
    @Cacheable(value="hotel_cache",key = "'hotel_'+#HotelId")
    public List<Comment> findCommentByHotelId(Integer HotelId) {
        log.info("根据酒店id为{}从数据库查询",HotelId);
        List<Comment> comments = commentMapper.findCommentByHotelId(HotelId);
        return comments;
    }

    @Override
    public void delectCommentById(Integer commentId) {
        commentMapper.delectCommentById(commentId);
    }
}
